package com.woniuxy.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @author: ideaAdmin
 * TODO: 基于Aspectj的xml模式切面 只需要配置通知即可
 * Date: 2022/1/20:16:13
 * Description:
 */

public class MyAspectj {
    //前置通知
    public void before(JoinPoint joinPoint){
        System.out.println("前置通知");
        System.out.println("当前被织入的增强目标是:" + joinPoint.getTarget().getClass().getName());
        System.out.println("被增强的方法是:" + joinPoint.getSignature().getName());
    }
    //后置通知
    public void afterReturning(JoinPoint joinPoint){
        System.out.println("后置通知:模拟事务提交,结束时间：" + System.currentTimeMillis());
    }

    //环绕通知
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("开始环绕通知");
        //获得方法的参数
        Object[] args = proceedingJoinPoint.getArgs();

        Object proceed = proceedingJoinPoint.proceed();

        System.out.println("环绕结束 结束时间：" + System.currentTimeMillis());
    }

    //异常通知
    public void afterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("异常通知");
        System.out.println("报错信息" + e.getMessage());
    }

    //最终通知
    public void after(JoinPoint joinPoint) throws InterruptedException {
        System.out.println("最终通知 模拟方法结束后释放资源 开始时间：" + System.currentTimeMillis());
        for (int i = 0; i < 100; i++) {
            Thread.sleep(10);
        }
        System.out.println("最终通知 模拟方法结束后释放资源 结束时间：" + System.currentTimeMillis());
    }

}
